<template>
  <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in EmployessList" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployessSimple } from '@/api/employees'
import { addDepartments, editDepartmentsDetail, getDepartmentDeatil } from '@/api/departments'
export default {
  props: {
    id: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    departmentsList: {
      required: true,
      type: Array
    }
  },
  data() {
    // 校验不能出现相同的name
    const validCode = (rule, value, callback) => {
      let testCode = this.departmentsList.map(item => item.code)
      if (this.isEdit) {
        testCode = this.departmentsList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (testCode.includes(value)) {
        callback(new Error(`已存在${value},请重新输入`))
      } else {
        callback()
      }
    }
    const valiName = (rule, value, callback) => {
      let testName = this.departmentsList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        const pid = this.departmentsList.find(item => item.id === this.id).pid
        // 根据pid找到所有同级
        testName = this.departmentsList.filter(item => item.pid === pid).filter(item => item.id !== this.id).map(item => item.name)
      }
      if (testName.includes(value)) {
        callback(new Error(`重复的${value},请重新输入`))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符内容' },
          { validator: valiName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符内容' },
          { validator: validCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符内容' }
        ],
        introduce: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符内容' }
        ]
      },
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      EmployessList: []
    }
  },

  created() {
    this.LoadingDepartmentsList()
    if (this.isEdit) {
      this.LoadingDepartmentsDetail()
    }
  },
  methods: {
    // 确定
    hSubmit() {
      // 进行兜底校验
      this.$refs.deptForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.hEdit() : this.hAdd()
      })
    },
    // 重置表单
    reset() {
      this.$refs.deptForm.resetFields()
    },
    // 取消
    hCancel() {
      this.$emit('close')
    },
    async hEdit() {
      try {
        const res = await editDepartmentsDetail(this.form)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    async LoadingDepartmentsList() {
      try {
        const res = await getEmployessSimple()
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.$message)
        this.EmployessList = res.data
      } catch (error) {
        console.log(error)
      }
    },
    async hAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.$emit('success')
        }
      } catch (error) {
        console.log(error)
      }
    },
    async LoadingDepartmentsDetail() {
      try {
        const res = await getDepartmentDeatil(this.id)
        // console.log(res)
        if (res.code === 10000) { this.form = res.data }
      } catch (error) {
        console.log(error)
      }
    }
    // async hEdit() {
    //   try {
    //     const res = await editDepartmentsDetail(this.form)
    //     console.log(res)
    //   } catch (error) {
    //     console.log(error)
    //   }
    // }
  }

}
</script>

<style lang="scss" scoped>

</style>
